import re
import requests
import lxml.html
import bs4 #import  BeautifulSoup
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from config import DB_URI
# from lxml import etree

engine = create_engine(DB_URI)
Base = declarative_base(engine)  # SQLORM基类
session = sessionmaker(engine)()  # 构建session对象


# header={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
#
# }

class Movie(Base):
    __tablename__ = 'movie'  # 表名
    id = Column(Integer, primary_key=True, autoincrement=True)
    moviename = Column(String(500))
    url= Column(String(500))


class Movieitem(Base):
    __tablename__ = 'movieitem'  # 表名
    id = Column(Integer, primary_key=True, autoincrement=True)
    translationName = Column(String(100))
    MovieName= Column(String(100))
    Age=Column(String(50))
    Address=Column(String(100))
    Type=Column(String(100))
    LanguageType=Column(String(100))
    ReleaseDate=Column(String(100))
    FileFormat=Column(String(50))
    VideoSize=Column(String(100))
    FileDB=Column(Integer)
    VideoTimeLong=Column(Integer)
    Director=Column(String(100))
    Starring=Column(String(200))
    Introduction=Column(String(5000))
    Subtitles=Column(String(100))


def getAddress():
    movie = session.query(Movie).all()
    return movie

def getMovieItem(url):
    r = requests.get(url)
    r.encoding = r.apparent_encoding

    soup = bs4.BeautifulSoup(r.text, "lxml")
    itemlist= soup.find('div', id='Zoom')
    #切割文本获取相应信息
    movieitemclass=Movieitem()
    movieitemclass.translationName =str(re.findall('◎译　　名.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.MovieName=str(re.findall('◎片　　名.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.Age=str(re.findall('◎年　　代.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.Type=str(re.findall('◎类　　别.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.LanguageType=str(re.findall('◎语　　言.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.Subtitles=str(re.findall('◎字　　幕.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.ReleaseDate=str(re.findall('◎上映日期.*?<br/>', str(itemlist)))[13:-7]
    movieitemclass.FileFormat=str(re.findall('◎文件格式.*?<br/>', str(itemlist)))[13:-7]
    movieitemclass.VideoSize=str(re.findall('◎视频尺寸.*?<br/>', str(itemlist)))[13:-7]
    movieitemclass.FileDB=str(re.findall('◎文件大小.*?<br/>', str(itemlist)))[13:-7]
    movieitemclass.VideoTimeLong=str(re.findall('◎片　　长.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.Director=str(re.findall('◎导　　演.*?<br/>', str(itemlist)))[23:-7]
    movieitemclass.Starring=str(re.findall('◎主　　演.*?<br/>◎', str(itemlist)))[23:-8].replace(" ","").replace("<br/>","")
    movieitemclass.Introduction=str(re.findall('简　　介.*?<br/>◎', str(itemlist)))[23:-8].replace(" ","")
    session.add(movieitemclass)  # 添加到session
    session.commit()  # 提交到数据库

    # imagelist=soup.findAll("img")
    #print(movieitemclass.translationName)
    # print(imagelist)



if __name__ == '__main__':
    lstAddress= getAddress()
    for ls in lstAddress:
        moviename= ls.moviename
        movieaddress =ls.url
        getMovieItem(movieaddress)
